package com.panghl.report;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author panghl
 * @Description 写excel
 **/
public class ExcelWrite {

    private static String path = "D:\\IdeaProjects\\Java-projects\\opExcel\\src";

    public static void writeExcel03() throws IOException {
        //1.创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //2.创建一个工作表
        Sheet sheet = workbook.createSheet("panghl-test-统计表");
        //3.创建行
        Row row1 = sheet.createRow(0);
        //4.创建单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日的测试次数");

        Cell cell112 = row1.createCell(1);
        cell112.setCellValue("测试时间");

        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue(1000);

        Cell cell22 = row2.createCell(1);
        String date = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(date);


        //生成一张表 （IO流）  03版本就是使用xls结尾！
        FileOutputStream fileOutputStream = new FileOutputStream("03.xls");
        //输出
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();

        System.out.println("文件生成完成！");
    }

    public static void writeExcel07() throws IOException {
        //1.创建一个工作簿
        Workbook workbook = new XSSFWorkbook();
        //2.创建一个工作表
        Sheet sheet = workbook.createSheet("panghl-test-统计表");
        //3.创建行
        Row row1 = sheet.createRow(0);
        //4.创建单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日的测试次数");

        Cell cell112 = row1.createCell(1);
        cell112.setCellValue("测试时间");

        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue(1000);

        Cell cell22 = row2.createCell(1);
        String date = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(date);


        //生成一张表 （IO流）  03版本就是使用xls结尾！
        FileOutputStream fileOutputStream = new FileOutputStream("07.xlsx");
        //输出
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();

        System.out.println("文件生成完成！");
    }

    //速度快
    public static void write03BigData() throws IOException {
        //时间
        long begin = System.currentTimeMillis();

        //创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("over...");
        FileOutputStream fileOutputStream = new FileOutputStream("03BigData.xls");
        workbook.write(fileOutputStream);

        long end = System.currentTimeMillis();

        System.out.println("执行耗费时间："+(end-begin)/1000+"s");
    }

    // 耗时较长
    public static void write07BigData() throws IOException {
        //时间
        long begin = System.currentTimeMillis();

        //创建一个工作簿
        Workbook workbook = new XSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("over...");
        FileOutputStream fileOutputStream = new FileOutputStream("07BigData.xlsx");
        workbook.write(fileOutputStream);

        long end = System.currentTimeMillis();

        System.out.println("执行耗费时间："+(end-begin)/1000+"s");
    }

    public static void write07BigDataSXSSF() throws IOException {
        //时间
        long begin = System.currentTimeMillis();

        //创建一个工作簿
        Workbook workbook = new SXSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("over...");
        FileOutputStream fileOutputStream = new FileOutputStream("07BigDataSXSSF.xlsx");
        workbook.write(fileOutputStream);
        //清除临时文件!
        ((SXSSFWorkbook)workbook).dispose();
        long end = System.currentTimeMillis();

        System.out.println("执行耗费时间："+(end-begin)/1000+"s");
    }

}
